package com.zzy.banian.crm.service;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzy.banian.crm.mapper.OpportunityMapper;
import com.zzy.banian.crm.domain.Opportunity;
import com.zzy.banian.crm.service.IOpportunityService;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;

/**
 * 商机 服务层实现
 * 
 * @author zhangzuoyi
 * @date 2019-05-04
 */
@Service("opportunity")
public class OpportunityServiceImpl implements IOpportunityService 
{
	@Autowired
	private OpportunityMapper opportunityMapper;

	/**
     * 查询商机信息
     * 
     * @param id 商机ID
     * @return 商机信息
     */
    @Override
	public Opportunity selectOpportunityById(Integer id)
	{
	    return opportunityMapper.selectOpportunityById(id);
	}
	
	/**
     * 查询商机列表
     * 
     * @param opportunity 商机信息
     * @return 商机集合
     */
	@Override
	public List<Opportunity> selectOpportunityList(Opportunity opportunity)
	{
	    return opportunityMapper.selectOpportunityList(opportunity);
	}
	
    /**
     * 新增商机
     * 
     * @param opportunity 商机信息
     * @return 结果
     */
	@Override
	public int insertOpportunity(Opportunity opportunity)
	{
		opportunity.setCreateBy(ShiroUtils.getLoginName());
		opportunity.setCreateTime(new Date());
	    return opportunityMapper.insertOpportunity(opportunity);
	}
	
	/**
     * 修改商机
     * 
     * @param opportunity 商机信息
     * @return 结果
     */
	@Override
	public int updateOpportunity(Opportunity opportunity)
	{
		opportunity.setUpdateBy(ShiroUtils.getLoginName());
		opportunity.setUpdateTime(new Date());
	    return opportunityMapper.updateOpportunity(opportunity);
	}

	/**
     * 删除商机对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteOpportunityByIds(String ids)
	{
		return opportunityMapper.deleteOpportunityByIds(Convert.toStrArray(ids));
	}

	@Override
	public List<Opportunity> findByCustomer(Integer customerId) {
		if(customerId != null){
			Opportunity opportunity=new Opportunity();
			opportunity.setCustomerId(customerId);
			return selectOpportunityList(opportunity);
		}
		return null;
	}
	
}
